package cn.jsc.config.rabbitmq;

import cn.jsc.constants.BaseConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ相关配置类 声明交换机，队列，交换机与队列的绑定关系
 */
@Configuration
public class RabbitmqConfig {

    //注册交换机
    @Bean
    public Exchange topicExchange(){
        //durable：设置持久化
        return ExchangeBuilder.topicExchange(BaseConstants.RabbitMQConstants.EXCHANGE_TOPIC_0907).durable(true).build();
    }

    //注册短信队列-用来发短信
    @Bean
    public Queue smsQueue(){
        return new Queue(BaseConstants.RabbitMQConstants.QUEUE_NAME_SMS, true, false, false);
    }

    //注册邮件队列-用来发邮件
    @Bean
    public Queue emailQueue(){
        return new Queue(BaseConstants.RabbitMQConstants.QUEUE_NAME_EMAIL, true, false, false);
    }

    //注册站内信队列-用来发邮件
    @Bean
    public Queue stationQueue(){
        return new Queue(BaseConstants.RabbitMQConstants.QUEUE_NAME_STATION, true, false, false);
    }

    //短信队列绑定到交换机
    @Bean
    public Binding bindSmsQueue(){
        return BindingBuilder.bind(smsQueue()).to(topicExchange()).with(BaseConstants.RabbitMQConstants.ROUTINGKEY_QUEUE_SMS).noargs();
    }

    //邮件队列绑定到交换机
    @Bean
    public Binding bindEmailQueue(){
        return BindingBuilder.bind(emailQueue()).to(topicExchange()).with(BaseConstants.RabbitMQConstants.ROUTINGKEY_QUEUE_EMAIL).noargs();
    }

    //站内信队列绑定到交换机
    @Bean
    public Binding bindStationQueue(){
        return BindingBuilder.bind(stationQueue()).to(topicExchange()).with(BaseConstants.RabbitMQConstants.ROUTINGKEY_QUEUE_STATION).noargs();
    }
}